#! /usr/bin/env python

import re

def recv_parser(recv):
	ret = []
	result = re.findall ('(.*) /(.*) HTTP/1.1', recv) #prende il tipo di richiesta e file.py?params
	try:
		result = result[0]
	except:
		print "Invalid request"
	try: 
		fname = result[1].split("?")[0]
	except:
		fname = result[1]

	ret.append(fname)
	
	if result[0] == 'GET':
		#richiesta get..
		if result[1].find("?") > -1:
			get_args = result[1][result[1].find("?")+1:]
		else:
			get_args = 0
			
		ret.append(get_args)
		ret.append(0) # parametri POST
		
	elif result[0] == 'POST':
		try:
			post_args = recv.split('\r\n\r\n')[1].rstrip()  #va messa le regexp anche per /n e basta!
			ret.append(0)  #parametri GET
			ret.append(post_args)
		except:
			ret.append(0) # crash... XD
			ret.append(0)
		
	recv = recv.split ("HTTP/1.1\r\n")[1].split("\r\n") #prende tutto quello dopo HTTP/1.1 e lo splitta
	req = {}
	cleng = False
	for line in recv:
		line_r = re.findall ('(.*): (.*)', line)
		if line_r != []:
			req[line_r[0][0]] = line_r[0][1]

	try:
		ret.append(req["Cookie"].rstrip())
	except:
		ret.append(0)
	return ret
	# ret = [0] = page, [1] = get, [2] = post, [3] = cookie
